﻿package org.openChart.sprite.groups
{
	import flash.display.SpreadMethod;
	import flash.display.Sprite;
	import org.openChart.display.chart.DisplayPieElement;
	import org.openChart.display.DisplayElement;
	import org.openChart.display.DisplayGroup;
	import org.openChart.meta.ChartType;
	import org.openChart.display.chart.DisplayBarElement;
	import org.openChart.sprite.BaseSprite;
	import org.openChart.sprite.CommandHandler;
	import org.openChart.sprite.ContainerSprite;
	import org.openChart.sprite.elements.Bar2d;
	import org.openChart.sprite.CommandHandler;
	import org.openChart.sprite.elements.ElementSprite;
	import org.openChart.sprite.elements.BarElementSprite;
	import org.openChart.sprite.elements.ElementFactory;
	import org.openChart.sprite.elements.Pie3d;
	import org.openChart.sprite.util.Helper;
	import org.openChart.sprite.Command;
	import flash.geom.Point;
	
	/**
	 * ...
	 * @author Chongyuan
	 */
	public class GroupSprite extends ContainerSprite
	{
		//protected var displayGroup:DisplayGroup;
		public function GroupSprite( displayGroup:DisplayGroup, commandHandler:CommandHandler) 
		{
			super(displayGroup, commandHandler);
			//this.displayGroup = displayGroup;
			this.init();
		}
		
		public function init():void
		{
			var displayGroup:DisplayGroup = displayObject as DisplayGroup;
			var displayElement:DisplayElement;
			removeAllChildren();
			
			for (var i:uint = 0 ; i < displayGroup.length; i++ )
			{
				displayElement = displayGroup.getAt(i) as DisplayElement;
				addElement(displayElement);
			}
		}
		
		public override function repaint():void
		{
			super.repaint();
		}
		
		public function addElement(displayElement:DisplayElement):void
		{
			var elementSprite:ElementSprite = ElementFactory.create(displayElement, this);
			this.addChild(elementSprite);
		}
		
		public function removeAllChildren():void
		{
			//TODO:remove all the childer of this group.
		}
		
		public override function processCommand(command:uint, target:BaseSprite):void
		{
			if (command == Command.TUNING_PIE_CHART)
			{
				var pie3d:Pie3d = target as Pie3d;
				var displayGroup:DisplayGroup = displayObject as DisplayGroup;
				//var j:int = -1;
				for (var i:uint = 0; i < length; i++ )
				{
					var dispalyElement:DisplayPieElement = getAt(i) as DisplayPieElement;
					dispalyElement.viewAngle += pie3d.moving;
					dispalyElement.viewAngle += pie3d.moving;
					if (dispalyElement.viewAngle > 90)
					{
						dispalyElement.viewAngle = 90;
					}
					else if (dispalyElement.viewAngle < 0)
					{
						dispalyElement.viewAngle = 0;
					}
				}
				repaint();
			}
		}
		
		public override function processMouseCommand(command:uint, target:BaseSprite, position:Point, object:Object):void
		{
			if (command == Command.TOOLTIP_HIDE || command == Command.TOOLTIP_SHOW)
			{
				commandHandler.processMouseCommand(command, target, position, object);
			}
		}
	}
	
}